package com.cpos.bkge.login;/*
*  @创建人 Cpos_wl
*  @创建时间 2020/5/29
   @描述
*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpos.bkge.base.ResultInfo;
import com.cpos.bkge.entity.CposBkUser;
import com.cpos.bkge.mapper.CposBkUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/bkge/register")
public class CposLoginController {

    @Autowired
    private CposBkUserMapper cposBkUserMapper;

    /*
        登录
    * */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public ResultInfo login(@RequestBody CposBkUser cposBkUser, HttpServletRequest request){

        ResultInfo resultInfo = new ResultInfo();

        QueryWrapper<CposBkUser> wrapper = new QueryWrapper<>();
       // wrapper.allEq(BeanMap.create(cposBkUser));
        wrapper.eq("user_name",cposBkUser.getUserName());
        wrapper.eq("pass_word",cposBkUser.getPassWord());

        CposBkUser cposBkUsers = cposBkUserMapper.selectOne(wrapper);

        if(cposBkUsers==null){
            resultInfo.setMsg("账号或密码不一致");
            resultInfo.setCode(1);
        }else{
            HttpSession session = request.getSession();
            session.setAttribute("username",cposBkUsers.getNickName());
            //设置30分钟
            session.setMaxInactiveInterval(1*60);
            resultInfo.setData(cposBkUsers);
            resultInfo.setMsg("登录成功");
        }
        return resultInfo;
    }
}
